package com.lib76.buildCode.control;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lib76.buildCode.dao.ProjectDao;
import com.lib76.buildCode.dto.AjaxResult;
import com.lib76.buildCode.dto.DtoFactory;
import com.lib76.buildCode.dto.ProjectData;
import com.lib76.buildCode.dto.TableResult;
import com.lib76.buildCode.model.Project;
import com.lib76.buildCode.service.BuilderServer;
import com.lib76.buildCode.service.ProjectCodeBuildFuture;
import com.lib76.buildCode.service.ProjectXmlServer;
import com.lib76.jsp.HttpServletUtils;

@Controller
public class IndexController {

	@RequestMapping("/")
	public String index(Model model){
		return "index";
	}
	
	/**
	 * 返回项目数据
	 * @return
	 */
	@RequestMapping("/project/data")
	@ResponseBody
	public TableResult<ProjectData> data(){
		TableResult<ProjectData> result = new TableResult<ProjectData>();
		result.setData(new ArrayList<ProjectData>());
		Collection<Project> data = ProjectDao.findAll();
		result.setDataTotal(Long.valueOf(data.size()));
		for(Project project : data){
			result.getData().add(DtoFactory.changeProjectData(project));
		}
		return result;
	}
	
	/**
	 * 编辑项目数据
	 * @param code
	 * @param model
	 * @return
	 */
	@RequestMapping("/project/edit")
	public String edit(String code,Model model){
		if(StringUtils.isBlank(code)){
			model.addAttribute("project", new Project());
		}else{
			model.addAttribute("project", ProjectDao.findByCode(code));
		}
		return "project/edit";
	}
	
	/**
	 * 保存项目数据
	 * @param project
	 * @return
	 */
	@RequestMapping("/project/save")
	@ResponseBody
	public AjaxResult save(Project project){
		AjaxResult result = new AjaxResult(1);
		ProjectDao.save(project);
		return result;
	}
	
	/**
	 * 保存项目数据为XML文件
	 * @param project
	 * @return
	 */
	@RequestMapping("/project/xml")
	@ResponseBody
	public AjaxResult xml(String code){
		AjaxResult result = new AjaxResult(0);
		if(StringUtils.isNotBlank(code)){
			Project project = ProjectDao.findByCode(code);
			if(project != null){
				if(ProjectXmlServer.buildXml(project, HttpServletUtils.getRealPath("/WEB-INF/project"))){
					result.setError(1);
				}else{result.setMsg("保存XML失败");}
			}else{
				result.setMsg("未找到"+code+"项目配置");
			}
		}else{result.setMsg("无效的参数Code");}
		return result;
	}
	
	@RequestMapping("/project/buildInfo")
	@ResponseBody
	public AjaxResult buildInfo(String code){
		AjaxResult result = new AjaxResult(0);
		ProjectCodeBuildFuture future = BuilderServer.getFuture(code);
		if(future != null){
			if(future.getResults().peek() != null){
				result.setError(1);
				result.setData(future.getResults().poll());
			}else if(StringUtils.isNotBlank(future.getProjectZipFilePath())){
				result.setError(2);
				result.setData(future.getProjectZipFilePath());
			}else{
				result.setError(3);
			}
		}else{
			result.setMsg("无法获取项目构建信息");
		}
		return result;
	}
	
	/**
	 * 构建项目代码
	 * @param code
	 * @param model
	 * @return
	 */
	@RequestMapping("/project/build")
	public String build(String code,Model model){
		Project project = ProjectDao.findByCode(code);
		if(project != null){
			//开始生成代码
			ProjectCodeBuildFuture future = BuilderServer.startBuild(project);
			BuilderServer.addFuture(future);
			model.addAttribute("project", project);
		}
		return "finish";
	}
}
